//
//  weatherManager.m
//  Cunli
//
//  Created by lilubin on 15/10/26.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "weatherManager.h"
#import "MyDataService.h"
#import "Config.h"
#import "NSString+MD5.h"

@interface weatherManager()
{
    NSDictionary *_weatherResult;
}

@end


@implementation weatherManager

+ (id)sharedManager {
    static dispatch_once_t once;
    static id instance;
    dispatch_once(&once, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (id)init {
    if ((self = [super init])) {
        [self loadWeatherData];
    }
    return self;
}

- (void)loadWeatherData
{
//    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *weatherDic = [NSMutableDictionary dictionaryWithCapacity:2];
    [weatherDic setObject:[Config getOwnUserID] forKey:@"userid"];
    [weatherDic setObject:[Config getOwnAreaids] forKey:@"areaids"];
    NSString *resultSign = [NSString getSignFromParam:weatherDic andSession:[Config getOwnSession]];
    [weatherDic setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"weather.php" httpMethod:@"GET" params:weatherDic fileDatas:nil complection:^(id result) {
        NSLog(@"weatherResult:%@", result);
        NSString *response = result[@"status"];
        _weatherResult = result;
        if ([response isEqualToString:@"success"]) {
            NSMutableArray *resultsArray = [NSMutableArray array];
            resultsArray = result[@"results"];
            NSMutableDictionary *weatherResults = [NSMutableDictionary dictionary];
            weatherResults = resultsArray[0];
            NSArray *weatherDataArray = weatherResults[@"weather_data"];
            _imgURL = [weatherDataArray[0] objectForKey:@"dayPictureUrl"];
            _weatherTitle = [weatherDataArray[0] objectForKey:@"temperature"];
            _setBtnUIKitts(_imgURL, _weatherTitle);
        }
        else if([response isEqualToString:@"error"])
        {
            
        }
    } failureAction:^{
    }];
}


@end
